<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/20
 * Time: 14:31
 */
function makeCard(){
    do {
        //获取用户数据信息
        echo "请输入用户名:";
        $username = trim(fgets(STDIN));
        echo "请输入学号:";
        $number = trim(fgets(STDIN));
        //连接数据库
        $dsn = 'mysql:dbname=school;host=192.168.175.2';
        $user = 'root';
        $password = '123456';
        $db = new PDO($dsn,$user,$password);
        try {
            $statement = $db->query("select rid from  reader");
            $temp = $statement->fetchAll();
            if ($statement) {
                echo "查询语句对了\n";
            } else if ($statement === false) {
                echo "查询语句出错\n";
            } else {
                echo "查询内容为空，请联系管理员：110\n";
            }
        } catch (PDOException   $e) {
            echo 'Connection failed:' . $e->getMessage();
            echo "\n";
        }
        do {
            $flag = false;
            /** @var TYPE_NAME $temp */
            foreach ($temp as $value) {
                if ($value['rid'] == $number) {
                    $flag = true;
                    break;
                }
            }
            if ($flag) {
                echo "学号已经存在,请重新输入学号:";
                $number = trim(fgets(STDIN));
            }
        } while ($flag);
        echo "\n";

//信息核实
        echo "您需要开卡信息：\n";
        echo "用户名:" . $username . "\n";
        echo "学号：" . $number . "\n";
        echo "输入y确认，任意键重新输入：";
        $abc = trim(fgets(STDIN));
        $abc = strtolower($abc);
    } while ($abc != 'y');
//添加新用户
    $statement = $db->exec("insert into reader (rname,rid) VALUES ('$username','$number')");
    echo "开卡成功，欢迎加入玛德图书馆成为机智会员！\n";
}